clear all
clear matrix
version 13
capture log close
set matsize 11000
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
global stub 	 = "/Volumes/Transcend/AWFP/ThirdChapter_revision"
global main		 = "$stub/data"
global inputcali = "$stub/data/CalibrationInput"	
global outputcali= "$stub/data/CalibrationOutput"	
global data_proc = "$stub/data"
global graphs    = "$stub/graphs"	
global tables    = "$stub/tables"
global do        = "$stub/do"
global excel     = "$stub/excel"	
global orig      = "$stub/orig"

cd "$main"
********************************************************************************
* VOLATILITY COMPARISON PART: analysis *****************************************
********************************************************************************
local year = 2020 	
global date = "2306"

import delimited using "${outputcali}/solution.csv", clear
sum v1
global sol = r(mean) /* optimal combination of parameter */

use "${outputcali}/${date}`year'/tosend_${date}`year'_${sol}.dta", clear
drop aux*
egen id = group(amr eventdate)
reshape long v tot_empl, i(id) j(bin)
merge m:1 amr using "${outputcali}/selectcities.dta"
drop if _merge!=3
drop _merge
drop if v==0			/* employment value is equal to 0 */
drop amr
ren newamr amr
sum amr 
global N1 = `r(max)'
********************************************************************************
forv n = 1(1)$N1 {
import delimited using "$outputcali/sharesout`n'.csv", clear
cap erase "$inputcali/shares`n'.csv"
cap erase "$outputcali/sharesout`n'.csv"
ren v1 idtime
ren v2 bin
ren v3 mu
ren v4 pred
ren v5 epsilon_old
ren v6 varonly
ren v7 devmu
ren v8 devvcv
gen epsilon = sqrt(_pi/2)*abs(epsilon_old)
xtset bin idtime
gen lagvar = l.varonly
replace lagvar = lagvar^.5
reg epsilon lagvar
gen r2 = e(r2)
pwcorr epsilon lagvar
gen corr = r(rho)
gen amr = `n'
if `n'==1{
save "$data_proc/temp.dta", replace
}
else{
append using "$data_proc/temp.dta"
save "$data_proc/temp.dta", replace
}
}

use "$data_proc/temp.dta", clear
keep corr amr dev*
duplicates drop
save "$data_proc/LOMmuR2.dta", replace
********************************************************************************
import delimited ${outputcali}/instantvolatilitydata.csv, clear 
gen idtime=_n
reshape long v, i(idtime) j(amr)
sum amr
xtset amr idtime
gen volatdata = sqrt(_pi/2)*abs(f.v-v)
ren v diffE
save ${data_proc}/comparevolatmodeldata.dta, replace

import delimited ${outputcali}/instantvolatilitymodel.csv, clear 
gen idtime=_n
reshape long v, i(idtime) j(amr)
sum amr 
xtset amr idtime
ren v rescaled
merge 1:1 amr idtime using ${data_proc}/comparevolatmodeldata.dta
drop if _merge!=3
drop _merge

merge m:1 amr using ${data_proc}/LOMmuR2.dta
drop _merge

ren amr newamr 
merge m:1 newamr using "${outputcali}/selectcities.dta"
drop _merge

merge m:1 amr using ${data_proc}/calibratedparameter.dta
drop if _merge!=3
drop _merge

drop corr

gen r2 = .
gen corr = .
gen beta = .
scatter rescaled volatdata
corr rescaled volatdata
reg rescaled volatdata 
egen id = group(amr)
drop amr
ren id amr
sum amr
forv n = 1(1)`r(max)'{
reg rescaled volatdata  if amr == `n'
replace r2 = e(r2) if amr==`n'
pwcorr rescaled volatdata  if amr == `n'
replace corr = `r(rho)' if amr == `n'
}

preserve
local var = "delta_model"
keep amr `var' dev* corr*
duplicates drop
reg corr `var'
sum
reg corr devmu
reg corr devvcv
reg devmu `var'
reg devvcv `var'
keep `var' corr amr
export delimited using ${outputcali}/r2deltaplot.csv, replace novarnames
restore
********************************************************************************
import delimited ${outputcali}/toexport1.csv, clear 
gen newamr = _n
replace v1 = ln(v1)
replace v2 = ln(v2)
replace v3 = ln(v3)
replace v4 = ln(v4)
ren v1 A1_basic 
ren v2 A2_varS 
ren v3 A3_varshrink 
ren v4 A4_vargrow 

sum

merge 1:1 newamr using "${outputcali}/selectcities.dta"
drop _merge

merge 1:1 amr using "${data_proc}/calibratedparameter.dta"
drop _merge

local varlist = "A2_varS A3_varshrink A4_vargrow"
foreach v of local varlist{
pwcorr A1_basic `v', sig
reg `v' logpop
}
* -> report the regression coefficients in the paper.
********************************************************************************
